<template>
  <page-wrap back dark hide-tab title="企业列表" stayStill>
    <view class="company-list">
      <view class="title">
        <text>{{ isCreate ? "企业信息未创建" : "企业信息已创建" }}</text>
        <text v-if="!isCreate" @click="toCreate" class="create-text"
          >创建企业</text
        >
      </view>
      <view class="list">
        <view
          class="list-item"
          v-for="(item, index) in companyList"
          :key="index"
          :class="{ active: index == activeIndex }"
          @click="handleClick(item, index)"
        >
          <image :src="item.companyLogo || defaultCompanylogo" mode="aspectFit" class="logo" />
          <view class="list-item-info">
            <view class="cname-all">
              <text v-if="item.companyCname"> {{ item.companyCname }}</text>
              <view class="pre" @click="handlePre(item)">预览</view>
            </view>

            <view class="detail">
              <text v-if="item.company_dq"
                >{{ item.company_dq }}<text class="divide">|</text></text
              >
              <text v-if="item.cxzName"
                >{{ item.cxzName }}<text class="divide">|</text></text
              >
              <text v-if="item.companyGm">{{ item.companyGm }}</text>
            </view>
            <view class="last_row">
              <text v-if="item.companyId">{{ item.companyId }}</text>
              <text v-if="item.idate">{{ item.idate }}</text>
            </view>
          </view>
        </view>
        <view v-if="isCreate" class="create-tips">
          请先完成企业创建
        </view>
      </view>
      <view class="bottom">
        <view
          v-if="isCreate"
          class="btn btn-primary btn-join"
          @click="toCreate"
        >
          创建企业
        </view>
        <view v-else class="btn btn-primary btn-join" @click="handleJoin">
          确认加入
        </view>
        <view class="tips" @click="handleCall('400-884-1001')">
          <text
            >客服热线：<text class="tel">400-884-1001</text>（8:00-18:00）</text
          >
        </view>
      </view>
    </view>
  </page-wrap>
</template>

<script>
import { getCompanyList, createBusinessCard } from "@/api/modules/company";
import personMixin from "../mixins/personDetail";

export default {
  mixins: [personMixin],
  data() {
    return {
      personId: uni.getStorageSync("person-id").slice(2) || undefined,
      companyList: [],
      defaultCompanylogo: this.$imgBase + "company/logo.jpg",
      activeIndex: 0,
      seletedItem: null,
      info: "",
      addTime: 0,
      isCreate: false,
    };
  },
  onShow() {
    // info = {
    //    avatar: '',
    // 		name: '',
    //    cname: '',
    // 		pnames: '',
    // 		email: '',
    // }
    this.info = uni.getStorageSync("recCardInfo");
    this.getPersonDetail(); // from personMixin
    this.getCompanyList();
  },
  methods: {
    handlePre(item) {
      uni.navigateTo({
        url:
          "/pagesSub/public/companyDetail/index?companyId=" + item.company_id ||
          "",
      });
    },
    handleClick(item, index) {
      this.activeIndex = index;
      this.seletedItem = item;
    },
    // 检查是否已存在公司
    getCompanyList() {
      // let params = {
      //   searchArr: JSON.stringify({
      //     cname: this.info.cname,
      //     // ComeAddress: 'app_new',
      //   }),
      //   conditionArr: JSON.stringify({
      //     page: 0,
      //     page_size: 9999
      //   })
      // }
      let params = {
        cname: this.info.cname,
        pageNo: 1,
        pageSize: 9999,
      };
      getCompanyList(params).then((res) => {
        if (res && res.code == 200) {
          if (res.data.length) {
            this.companyList = res.data;
            this.seletedItem = this.companyList[this.activeIndex];
            this.isCreate = false;
          } else {
            this.isCreate = true;
          }
        } else {
          uni.showToast({
            title: res.status_desc,
            icon: "none",
          });
        }
      });
    },
    handleJoin() {
      let isStop = new Date(this.seletedItem.stopdate) < new Date();
      let condition1 = this.seletedItem.ComeAddress == "app_new";
      let condition2 = this.seletedItem.ComeAddress == "ylt";
      let condition3 = this.seletedItem.gjVerId == "4731508921033609";
      let condition4 = this.seletedItem.gj == "1" && isStop;
      let condition5 = this.seletedItem.gj == "3" && isStop;
      let condition6 = this.seletedItem.issys == "9";
      let condition7 = this.seletedItem.gj != "1";
      let condition8 = this.seletedItem.gj != "3";
      if (
        condition1 ||
        condition2 ||
        condition3 ||
        condition4 ||
        condition5 ||
        condition6 ||
        condition7 ||
        condition8
      ) {
        this.jionInBusinessCard();
      } else {
        uni.showModal({
          title: "提示",
          content:
            "您已开通云聘服务，无法使用小程序，详情请联系客服0755—86153516",
          showCancel: false,
          success: (res) => {},
        });
      }
    },
    jionInBusinessCard() {
      // let params = {
      //   person_id: this.personId,
      //   company_id: this.seletedItem.companyId,
      //   personArr: JSON.stringify({
      //     pic: this.info.avatar,
      //     iname: this.info.name,
      //     mobile: this.userInfo.shouji, // from personMixin
      //     pnames: this.info.pnames,
      //     email: this.info.email,
      //   }),
      //   companyArr: JSON.stringify({}),
      //   conditionArr: JSON.stringify({
      //     source: 'mp'
      //   }),
      // }
      let params = {
        companyId: this.seletedItem.companyId,
        personId: this.personId,
        personArr: JSON.stringify({
          pic: this.info.avatar,
          iname: this.info.name,
          mobile: this.userInfo.shouji, // from personMixin
          pnames: this.info.pnames,
          email: this.info.email,
        }),
      };
      uni.showLoading();
      createBusinessCard(params)
        .then((res) => {
          // this.addTime++
          // if(this.addTime < 2) { // 已绑定过企业再次绑定返回数据异常，再次请求正常
          //   this.jionInBusinessCard();
          //   return;
          // }
          // this.addTime = 0;
          uni.hideLoading();
          if (res && res.code == 200) {
            // uni.removeStorageSync('recCardInfo');
            uni.setStorageSync("company-id", this.seletedItem.companyId);
            // uni.showToast({
            //   title: "加入成功",
            //   icon: "none",
            // });
            setTimeout(() => {
              uni.navigateTo({
                url: "/pagesSub/company/selectCert/index",
              });
            }, 1500);
          } else {
            uni.showToast({
              title: res.status_desc,
              icon: "none",
            });
          }
        })
        .catch((err) => {
          uni.hideLoading();
          uni.showToast({
            title: err,
            icon: "none",
          });
        });
    },
    // 拨打电话
    handleCall(mobile) {
      uni.makePhoneCall({
        phoneNumber: mobile,
      });
    },
    toCreate() {
      uni.navigateTo({
        url: "/pagesSub/company/create/index",
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.company-list {
  flex: 1;
  padding: $uni-spacing-col-big;
  padding-bottom: calc(260rpx + constant(safe-area-inset-bottom));
  padding-bottom: calc(260rpx + env(safe-area-inset-bottom));
  background-color: $uni-bg-color;

  .title {
    margin-bottom: $uni-spacing-col-lg;
    font-size: 48rpx;
    font-weight: bold;
  }
  .create-text {
    float: right;
    padding-top: 14rpx;
    font-size: $uni-font-size-base;
    color: #f75a3e;
  }
  .list-item {
    display: flex;
    margin-bottom: $uni-spacing-col-big;
    padding: $uni-spacing-col-big;
    border-radius: $uni-border-radius-lg;
    box-shadow: 0 16rpx 30rpx 10rpx #eee;
    &.active {
      border: 4rpx solid #f75a3e;
    }
    .list-item-info {
      flex: 1;
      width: 0;
    }
    .logo {
      flex-shrink: 0;
      width: 140rpx;
      height: 140rpx;
      margin-right: 20rpx;
      border-radius: 50%;
    }
    .cname-all {
      display: flex;
      justify-content: space-between;
      margin-bottom: $uni-spacing-col-base;
      font-size: $uni-font-size-base;
      font-weight: bold;
      color: #000;
    }
    .detail {
      font-size: $uni-font-size-sm;
      color: $uni-text-color-999;
      margin-bottom: $uni-spacing-col-base;
    }
    .last_row {
      display: flex;
      justify-content: space-between;
      font-size: $uni-font-size-sm;
      color: $uni-text-color-999;
    }
    .pre {
      display: flex;
      text-align: center;
      justify-content: center;
      align-items: flex-end;
      padding: 0rpx 14rpx;
      font-size: 20rpx;
      color: #f75a3e;
      border: 2rpx solid #f75a3e;
      border-radius: 30rpx;
      width: 100rpx;
      height: 60rpx;
      line-height: 60rpx;
      margin-left: 8rpx;
      
    }
  }
  .create-tips {
    padding-top: 100rpx;
    text-align: center;
  }
  .divide {
    margin: 0 12rpx;
  }
  .bottom {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    z-index: 10;
    background-color: $uni-bg-color;
    padding: 32rpx;
    padding-bottom: calc(32rpx + constant(safe-area-inset-bottom));
    padding-bottom: calc(32rpx + env(safe-area-inset-bottom));
    .btn {
      width: 100%;
      margin-bottom: 40rpx;
    }
    .tips {
      text-align: center;
      font-size: $uni-font-size-sm;
      color: $uni-text-color-999;
      .tel {
        color: #f75a3e;
      }
    }
  }
}
</style>
